#ifndef __STACK_H__
#define __STACK_H__
typedef struct stack{ //用链表实现的栈，栈顶元素为当前stack->last->value
	double value;
	struct stack* last;
}stack;

stack* create_stack() { //创建链表的头
	stack* s = malloc((sizeof(stack)));
	s->last = NULL;
	s->value = 0;
}

int stack_length(stack *s) {
	int l = 0;
	while(s->last != NULL) {
		s = s->last;
		l++;
	}
	return l;
}

double get(stack *s) { //获取栈顶元素
	return s->last->value;
}

double pop(stack **s) { //出栈
	stack* temp_s = *s;
	*s = (*s)->last;
	free(temp_s);
	return (*s)->value;
}

void push(stack **s,double n) { //入栈
	stack* new_s = malloc((sizeof(stack)));
	(*s)->value = n;
	new_s->last = *s;
	*s = new_s;
}

int is_empty(stack *s) { //判断栈是否为空(只有链表头)
	return s->last == NULL;
}

void print_stack(stack *s) { //打印栈的内容(数字格式)
	while(!is_empty(s)) {
		printf("%.1lf | ",s->last->value);
		s = s->last;
	}
	printf("\n");
}

void print_stack_c(stack *s) { //打印栈的内容(字符格式)
	while(!is_empty(s)) {
		printf("%c | ",(char)(s->last->value));
		s = s->last;
	}
	printf("\n");
}

#endif

/* example 

	stack* s = create_stack();
	for (int i = 0; i < 10; ++i)
	{
		push(&s,i);
	}
	for (int i = 0; i < 5; ++i)
	{
		printf("%d ",pop(&s));
	}
	for (int i = 0; i < 10; ++i)
	{
		push(&s,i);
	}
	while(s->last != NULL) {
		printf("%d ",pop(&s));
	}
	printf("ok");
 */